#include "Scene.h"

Scene::Scene(char const* bgfile, char const*mapfile, char const*floorfile) :
  BackgroundImageFile(NULL), CollisionMapImageFile(NULL), FloorImageFile(NULL),
  Background(NULL), CollisionMap(NULL), Floor(NULL)
{
  if(bgfile) {
    int len = strlen(bgfile);
    BackgroundImageFile = new char[len];
    if (BackgroundImageFile)
      strcpy(BackgroundImageFile,bgfile);
    else
      cout << "BackgroundImageFile not allocated\n" << endl;
  }

  if(mapfile) {
    int len = strlen(mapfile);
    CollisionMapImageFile = new char[len];
    if (CollisionMapImageFile)
      strcpy(CollisionMapImageFile,mapfile);
    else
      cout << "CollisionMapImageFile not allocated\n" << endl;
  }

  if(floorfile) {
    int len = strlen(floorfile);
    FloorImageFile = new char[len];
    if (FloorImageFile)
      strcpy(FloorImageFile,floorfile);
    else
      cout << "FloorImageFile not allocated\n" << endl;
  }

  MapRect.x = MAP_POS_X;
  MapRect.y = MAP_POS_Y;
  MapRect.w = MAP_SIZE_X;
  MapRect.h = MAP_SIZE_Y;
}

int Scene::InitScene() {
  if(BackgroundImageFile) {
    if (!Background) Background = IMG_Load(BackgroundImageFile);
    if (!Background) {
      printf("Unable to load background image: %s\n", SDL_GetError());
      exit(1);
    }
  }

  if(CollisionMapImageFile) {
    if (!CollisionMap) CollisionMap = IMG_Load(CollisionMapImageFile);
    if (!CollisionMap) {
      printf("Unable to load collision map image: %s\n", SDL_GetError());
      exit(1);
    }
  }

  if(FloorImageFile) {
    if (!Floor) Floor = IMG_Load(FloorImageFile);
    if (!Floor) {
      printf("Unable to load floor image: %s\n", SDL_GetError());
      exit(1);
    }
  }
}

Scene::~Scene() {
  if (BackgroundImageFile) delete[] BackgroundImageFile;
  if (Background) SDL_FreeSurface(Background);
  if (CollisionMapImageFile) delete[] CollisionMapImageFile;
  if (CollisionMap) SDL_FreeSurface(CollisionMap);
  if (FloorImageFile) delete[] FloorImageFile;
  if (Floor) SDL_FreeSurface(Floor);
}

